home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-04-30 | 10.5 KB | 469 lines | [TEXT/MPS ] |
- /*
- File: OTTcpPitchSample.cp
-
- Contains: Tcp pitch sample.
-
- Copyright: © 1993-1997 by Apple Computer, Inc., all rights reserved.
-
- */
-
-
- // OT TCP Pitch Test Program (as an SIOW app)
-
- #include <QuickDraw.h>
- #include <stdio.h>
- #include <StdLib.h>
- #include <TextUtils.h>
- #include <strings.h>
- #include <String.h>
- #include <Events.h>
- #include <Menus.h>
- #include <Devices.h>
- #include <OpenTransport.h>
- #include <OpenTptInternet.h>
-
- #ifndef T_DISCON_IND
- #define T_DISCON_IND 128
- #endif
-
-
- /*******************************************************************************
- ** GLOBAL VARIABLES
- ********************************************************************************/
-
- #define kMaxDataLen 256
-
- const size_t kMyPoolSize = 60000;
-
- InetPort gCatchPort = 0;
- InetHost gCatchIpAddr = 0;
- InetPort gPitchPort = 0;
- InetHost gPitchIpAddr = 0;
-
- unsigned short gBindCompleted = 0;
- unsigned short gCallRcvOrdDiscon = 0;
-
- struct InetAddress sndsin, rcvsin, reqsin, retsin;
- char data[kMaxDataLen];
- char defaultData[] = "Go Cal, beat Stanford !!!";
-
- /*******************************************************************************
- ** Function Prototypes
- ********************************************************************************/
-
- void Inits();
- void CleanUp();
- void Idle();
- void DoIt();
-
-
- /*******************************************************************************
- ** main function
- ********************************************************************************/
-
- void main()
- {
- Inits();
- DoIt();
- CleanUp();
- }
-
- /*******************************************************************************
- ** Initialize Quickdraw and ASLM
- ********************************************************************************/
-
- void Inits()
- {
- InitGraf(&qd.thePort);
- if (InitOpenTransport() != kOTNoError)
- {
- fprintf(stderr, "OTTcpPitch: Could not initialize ASLM, exiting\n");
- exit(1);
- }
- }
-
- /*******************************************************************************
- ** ShowEndpointOptions
- ********************************************************************************/
-
- void ShowEndpointOptions(TEndpoint* ep)
- {
- /*
- * This function retrieves and displays
- * the IP and TCPP endpoint options for this endpoint.
- */
-
- OSStatus err;
- TOptMgmt* ret = (TOptMgmt*)OTAlloc(ep, T_OPTMGMT, T_OPT, &err);
-
-
- do
- {
- fprintf(stderr, "Current Readable IP Option Settings for endpoint @ %08lX:\n", ep);
- if ( ret == NULL )
- {
- fprintf(stderr, "ERROR: could not allocate TOptMgmt structure (%d)\n", err);
- break;
- }
- //
- // Get the current IP options
- //
- TOptMgmt req;
- TOptionHeader option;
-
- option.len = kOTOptionHeaderSize;
- option.level = INET_IP;
- option.name = T_ALLOPT;
-
- req.opt.buf = (UInt8*)&option;
- req.opt.len = kOTOptionHeaderSize;
- req.flags = T_CURRENT;
-
- err = ep->OptionManagement(&req, ret);
- if ( err != kOTNoError )
- {
- fprintf(stderr, "ERROR: OptionManagement T_CURRENT request returned %d\n", err);
- break;
- }
- //
- // Now, let's print the options
- //
- {
- TOption* opt = (TOption*)ret->opt.buf;
- char string[512];
-
- err = OTCreateOptionString("ip", &opt, ret->opt.buf + ret->opt.len,
- string, sizeof(string));
-
- if ( err == kOTNoError )
- {
- char* str = string;
- size_t len = 0;
- while ( true )
- {
- char* temp = strchr(str, ',');
- if ( temp == NULL )
- {
- fprintf(stderr, "%s\n", str);
- break;
- }
- if ( len + temp - str + 1 > 80 )
- {
- fprintf(stderr, "\n");
- if ( *str == ' ' )
- str += 1;
- len = 0;
- }
- fprintf(stderr, "%*.*s", temp - str + 1, temp - str + 1, str);
- len += temp - str + 1;
- str = temp + 1;
- }
- }
- }
-
- TOption* opt = OTFindOption(ret->opt.buf, ret->opt.len, INET_IP, IP_TTL);
-
- if ( opt == NULL )
- fprintf(stderr, "ERROR:OptionManagement did not have IP_TTL in returned options\n");
-
- //
- // Get the current TCP options
- //
- fprintf(stderr, "Current Readable TCP Option Settings for endpoint @ %08lX:\n", ep);
- option.len = kOTOptionHeaderSize;
- option.level = INET_TCP;
- option.name = T_ALLOPT;
-
- req.opt.buf = (UInt8*)&option;
- req.opt.len = kOTOptionHeaderSize;
- req.flags = T_CURRENT;
-
- err = ep->OptionManagement(&req, ret);
- if ( err != kOTNoError )
- {
- fprintf(stderr, "ERROR: OptionManagement T_CURRENT request returned %d\n", err);
- break;
- }
- //
- // Now, let's print the options
- //
- {
- TOption* opt = (TOption*)ret->opt.buf;
- char string[512];
-
- err = OTCreateOptionString("tcp", &opt, ret->opt.buf + ret->opt.len,
- string, sizeof(string));
-
- if ( err == kOTNoError )
- {
- char* str = string;
- size_t len = 0;
- while ( true )
- {
- char* temp = strchr(str, ',');
- if ( temp == NULL )
- {
- fprintf(stderr, "%s\n", str);
- break;
- }
- if ( len + temp - str + 1 > 80 )
- {
- fprintf(stderr, "\n");
- if ( *str == ' ' )
- str += 1;
- len = 0;
- }
- fprintf(stderr, "%*.*s", temp - str + 1, temp - str + 1, str);
- len += temp - str + 1;
- str = temp + 1;
- }
- }
- fprintf(stderr, "\n");
- }
-
- } while ( false );
- OTFree(ret, T_OPTMGMT);
- }
-
-
- /*******************************************************************************
- ** Clean up at the end
- ********************************************************************************/
-
- void CleanUp()
- {
- CloseOpenTransport();
- }
-
- /*******************************************************************************
- ** Idle
- ********************************************************************************/
-
- void Idle()
- {
- SystemTask();
- }
-
- /*******************************************************************************
- ** EventHandler
- ********************************************************************************/
-
- pascal void EventHandler(void*, OTEventCode event, OTResult, void*)
- {
- OTEventCode tempevent = 0;
-
- switch ( event )
- {
- case T_BINDCOMPLETE:
- gBindCompleted = 1;
- break;
- case T_ORDREL:
- gCallRcvOrdDiscon = 1;
- break;
- default:
- DebugStr("\pEventHandler got unexpected event");
- break;
- }
- return;
- }
-
- /*******************************************************************************
- ** DoIt
- ********************************************************************************/
-
- void DoIt()
- {
- TEndpoint* ep = NULL;
- TEndpointInfo info;
- TBind req, ret;
- TCall sndcall, rcvcall;
- OSStatus err = kOTNoError;
- long myport = 0;
- InetHost myaddr = 0;
- char mystr[255];
- long bytes = 0;
- OTFlags flags = 0;
-
- myport = 0;
- fprintf(stderr, "Pitch port ? (enter TCP port number)\n");
- if ( gets(mystr) != 0 )
- {
- stringtonum(mystr, &myport);
- gPitchPort =(InetPort) myport;
- }
- myaddr = 0;
- fprintf(stderr, "Pitch IP address ? (enter IP address)\n");
- if ( gets(mystr) != 0 )
- {
- if (OTInetStringToHost(mystr, &myaddr) == 0)
- {
- gPitchIpAddr = (InetHost) myaddr;
- }
- }
- myport = 0;
- fprintf(stderr, "Catch port ? (enter port number)\n");
- if ( gets(mystr) != 0 )
- {
- stringtonum(mystr, &myport);
- gCatchPort = (InetPort) myport;
- }
- myaddr = 0;
- fprintf(stderr, "Catch IP address ? (enter IP address)\n");
- if ( gets(mystr) != 0 )
- {
- if ( OTInetStringToHost(mystr, &myaddr) == 0 )
- {
- gCatchIpAddr = (InetHost) myaddr;
- }
- }
-
- fprintf(stderr, "What should I send ? (enter data string)\n");
- if ( gets(data) == 0 )
- {
- strcpy(data, defaultData);
- fprintf(stderr, "sending default data: <%s>\n", data);
- }
- data[strlen(data)] = '\n';
-
- OTInetHostToString(gCatchIpAddr, mystr);
- fprintf(stderr, "The program will send a packet to <%s:%d> on port <%d>\n", mystr, gCatchPort, gPitchPort);
-
- memset(&sndsin, 0, sizeof(struct InetAddress));
- memset(&rcvsin, 0, sizeof(struct InetAddress));
- memset(&sndcall, 0, sizeof(TCall));
- memset(&rcvcall, 0, sizeof(TCall));
- memset(&reqsin, 0, sizeof(struct InetAddress));
- memset(&rcvsin, 0, sizeof(struct InetAddress));
- memset(&req, 0, sizeof(TBind));
- memset(&ret, 0, sizeof(TBind));
-
- OTInitInetAddress(&sndsin, gCatchPort, gCatchIpAddr);
-
- do
- {
- //
- // Now create a TCP
- //
- ep = OTOpenEndpoint(OTCreateConfiguration(kTCPName), 0, &info, &err);
-
- if ( ep == NULL || err != kOTNoError )
- {
- ep = NULL;
- fprintf(stderr,"ERROR: OpenEndpoint(\"TCP\") failed with %d\n", err);
- break;
- }
-
- err = ep->SetSynchronous();
- if ( err != kOTNoError )
- {
- fprintf(stderr, "ERROR: SetSynchronous() failed with %d\n", err);
- break;
- }
-
- //
- // Install notifier we're going to use for testing
- //
- err = ep->InstallNotifier(&EventHandler, 0);
- if ( err != kOTNoError )
- {
- fprintf(stderr, "ERROR: InstallNotifier() failed with %d\n", err);
- break;
- }
-
- //
- // Try to bind
- //
- OTInitInetAddress(&reqsin, gPitchPort, gPitchIpAddr);
-
- req.addr.len = sizeof(struct InetAddress);
- req.addr.buf = (unsigned char *) &reqsin;
- req.qlen = 2; // don't care for tcp
- ret.addr.maxlen = sizeof(struct InetAddress);
- ret.addr.buf = (unsigned char *) &retsin;
-
- // bind TCP to current address and port
- err = ep->Bind(&req, &ret);
- if ( err != kOTNoError )
- {
- fprintf(stderr, "ERROR: Bind() failed with %d\n", err);
- break;
- }
- err = ep->SetSynchronous();
- if ( err != kOTNoError )
- {
- fprintf(stderr, "ERROR: SetSynchronous() failed with %d\n", err);
- break;
- }
-
- ShowEndpointOptions(ep);
-
- sndcall.addr.len = sizeof(struct InetAddress);
- sndcall.addr.buf = (unsigned char *) &sndsin;
-
- rcvcall.addr.maxlen = sizeof(struct InetAddress);
- rcvcall.addr.buf = (unsigned char *) &rcvsin;
-
- err = ep->Connect(&sndcall, &rcvcall);
- if ( err != kOTNoError )
- {
- fprintf(stderr, "ERROR: Connect() failed with %d\n", err);
- break;
- }
-
- bytes = ep->Snd(data, strlen(data), flags);
- if ( bytes >= 0 )
- {
- fprintf(stderr, "Sent packet\nbytes: <%d> data: %s", bytes, data);
- }
- else
- {
- fprintf(stderr, "ERROR: Snd() failed with %d\n", bytes);
- }
- Idle();
- } while (false);
-
- if ( ep != NULL )
- {
- err = ep->SndOrderlyDisconnect();
- if ( err != kOTNoError )
- {
- if ( err == kOTLookErr )
- fprintf(stderr, "SndOrderlyDisconnect() returns %d\n", err);
- else
- fprintf(stderr, "ERROR: SndOrderlyDisconnect() failed with %d\n", err);
- }
-
- while (gCallRcvOrdDiscon == 0)
- Idle();
- err = ep->RcvOrderlyDisconnect();
- if ( err != kOTNoError )
- {
- fprintf(stderr, "ERROR: RcvOrderlyDisconnect() failed with %d\n", err);
- }
-
- //
- // Remove notifier
- //
- ep->RemoveNotifier();
- //
- // Try to Unbind
- //
- /*
- err = ep->Unbind();
- if ( err != kOTNoError )
- {
- fprintf(stderr, "ERROR: Unbind() returned %d\n", err);
- }
- */
- //
- // Get rid of endpoint.
- //
- err = OTCloseProvider(ep);
- if ( err != kOTNoError )
- {
- fprintf(stderr, "ERROR: CloseEndpoint() failed with %d\n", err);
- }
- }
- fprintf(stderr, "Bye\n");
- }
-